home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / sox.zip / SNDRTOOL.C < prev    next >
C/C++ Source or Header  |  1992-06-15  |  4KB  |  161 lines

  1. /*
  2.  * Sounder/Sndtool format handler: W V Neisius, February 1992
  3.  */
  4.  
  5. #include <math.h>
  6. #include "st.h"
  7.  
  8. extern errno;
  9. extern char *sys_errlist[];
  10.  
  11. /* Private data used by writer */
  12. struct sndpriv {
  13.         unsigned long nsamples;
  14. };
  15.  
  16. #ifndef    SEEK_CUR
  17. #define    SEEK_CUR    1
  18. #endif
  19.  
  20. /*======================================================================*/
  21. /*                         SNDSTARTREAD                                */
  22. /*======================================================================*/
  23.  
  24. sndtstartread(ft)
  25. ft_t ft;
  26. {
  27. struct sndpriv *p = (struct sndpriv *) ft->priv;
  28.  
  29. char buf[97];
  30.  
  31. int rate;
  32. int i;
  33.  
  34. rate = 0;
  35.  
  36. /* determine file type */
  37.         /* if first 5 bytes == SOUND then this is probably a sndtool sound */
  38.         /* if first word (16 bits) == 0 
  39.          and second word is between 4000 & 25000 then this is sounder sound */
  40.         /* otherwise, its probably raw, not handled here */
  41.  
  42. if (fread(buf, 1, 2, ft->fp) != 2)
  43.     fail("SND: unexpected EOF");
  44. if (strncmp(buf,"\0\0",2) == 0)
  45.     {
  46.     /* sounder */
  47.     rate = rlshort(ft);
  48.     if (rate < 4000 || rate > 25000 )
  49.         fail ("SND: sample rate out of range");
  50.     fseek(ft->fp,4,SEEK_CUR);
  51.     }
  52. else
  53.     {
  54.     /* sndtool ? */
  55.     fread(&buf[2],1,6,ft->fp);
  56.     if (strncmp(buf,"SOUND",5))
  57.         fail ("SND: unrecognized SND format");
  58.     fseek(ft->fp,12,SEEK_CUR);
  59.     rate = rlshort(ft);
  60.     fseek(ft->fp,6,SEEK_CUR);
  61.     if (fread(buf,1,96,ft->fp) != 96)
  62.         fail ("SND: unexpected EOF in SND header");
  63.     report ("%s",buf);
  64.     }
  65.  
  66. ft->info.channels = 1;
  67. ft->info.rate = rate;
  68. ft->info.style = UNSIGNED;
  69. ft->info.size = BYTE;
  70.  
  71. }
  72.  
  73. /*======================================================================*/
  74. /*                         SNDTSTARTWRITE                               */
  75. /*======================================================================*/
  76. sndtstartwrite(ft)
  77. ft_t ft;
  78. {
  79. struct sndpriv *p = (struct sndpriv *) ft->priv;
  80.  
  81. /* write header */
  82. ft->info.style = UNSIGNED;
  83. ft->info.size = BYTE;
  84. p->nsamples = 0;
  85. sndtwriteheader(ft, 0);
  86.  
  87. }
  88. /*======================================================================*/
  89. /*                         SNDRSTARTWRITE                               */
  90. /*======================================================================*/
  91. sndrstartwrite(ft)
  92. ft_t ft;
  93. {
  94. struct sndpriv *p = (struct sndpriv *) ft->priv;
  95.  
  96. /* write header */
  97. ft->info.style = UNSIGNED;
  98. ft->info.size = BYTE;
  99.  
  100. /* sounder header */
  101. wlshort (ft,0); /* sample size code */
  102. wlshort (ft,ft->info.rate);     /* sample rate */
  103. wlshort (ft,10);        /* volume */
  104. wlshort (ft,4); /* shift */
  105. }
  106.  
  107. /*======================================================================*/
  108. /*                         SNDTWRITE                                     */
  109. /*======================================================================*/
  110.  
  111. sndtwrite(ft, buf, len)
  112. ft_t ft;
  113. long *buf, len;
  114. {
  115.     struct sndpriv *p = (struct sndpriv *) ft->priv;
  116.     p->nsamples += len;
  117.     rawwrite(ft, buf, len);
  118. }
  119.  
  120. /*======================================================================*/
  121. /*                         SNDTSTOPWRITE                                */
  122. /*======================================================================*/
  123.  
  124. sndtstopwrite(ft)
  125. ft_t ft;
  126. {
  127. struct sndpriv *p = (struct sndpriv *) ft->priv;
  128.  
  129. /* fixup file sizes in header */
  130. if (fseek(ft->fp, 0L, 0) != 0)
  131.     fail("can't rewind output file to rewrite SND header");
  132. sndtwriteheader(ft, p->nsamples);
  133. }
  134.  
  135. /*======================================================================*/
  136. /*                         SNDTWRITEHEADER                              */
  137. /*======================================================================*/
  138. sndtwriteheader(ft,nsamples)
  139. ft_t ft;
  140. long nsamples;
  141. {
  142. char name_buf[97];
  143.  
  144. /* sndtool header */
  145. fputs ("SOUND",ft->fp); /* magic */
  146. fputc (0x1a,ft->fp);
  147. wlshort (ft,(long)0);  /* hGSound */
  148. wllong (ft,nsamples);
  149. wllong (ft,(long)0);
  150. wllong (ft,nsamples);
  151. wlshort (ft,ft->info.rate);
  152. wlshort (ft,0);
  153. wlshort (ft,10);
  154. wlshort (ft,4);
  155. sprintf (name_buf,"%s - File created by Sound Exchange",ft->filename);
  156. fwrite (name_buf, 1, 96, ft->fp);
  157.  
  158. }
  159.  
  160.  
  161.